package com.zn.task;

import com.alibaba.fastjson.JSONObject;
import com.zn.entity.OptionTradingGoods;
import com.zn.service.MdInfoService;
import com.zn.util.Constant;
import com.zn.util.DateUtils;
import com.zn.vo.Response;
import com.zn.web.rpc.TradeDateCall;
import com.zn.websocket.server.ChannelGroupsServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author YinJiaWei
 * @version 2018/11/7 14:26
 */
@Component
public class ExecuteGoodsTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExecuteGoodsTask.class);
    @Autowired
    private MdInfoService mdInfoService;
    @Autowired
    private ChannelGroupsServer channelGroupsServer;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Scheduled(cron="0 15 09 * * ?")   //每5秒执行一次
    public void initTarget(){
      try{
            String nowDate= DateUtils.convertDateToString(new Date());
            /** 查询标的证券信息，每天只需要查询一次**/
            Set<Object> targetCodes=redisTemplate.opsForHash().keys(Constant.TARGET_CODE_LIST);
            for(Object targetCode:targetCodes){
                Set<String> monthSet=redisTemplate.opsForSet().members(Constant.INSTRUMENT_MONTH_SET+targetCode.toString()+":"+nowDate);
                if(monthSet.size()>0){
                    mdInfoService.saveTgAndInstrumentMonth(targetCode.toString(),monthSet);
                }
            }
        } catch (Exception e){
            LOGGER.info("initTarget Error ..............:",e);
        }
    }

    /**
     * 定时初始化通道渠道、交易商品信息、交易日信息
     */
    @Scheduled(cron="0 16 09 * * ?")   //每天早上九点执行一次
    public void getEtfMdInfo(){
        try {
            channelGroupsServer.Initialization();
        }catch (Exception e){
            LOGGER.info("getEtfMdInfo Error ..............:", e);
        }
    }

}
